package basic.thread.juc.chapter1;
/**
 *  一、volatile 关键字：当多个线程进行共享操作时，可以保证内存中的数据可见‘
 *                       相较于synchronized是一种较为轻量级的同步策略。
 *  注意:
 *     1. volatile 不具备"互斥性"
 *     2. volatile  不能保证变量的"原子性"
 *
 *
 *
 * */
public  class TestVolatile{

    public static void main(String[] args) {

        ThreadDemo1 td = new ThreadDemo1();
        Thread thread = new Thread(td);
        thread.start();

        while (true){
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(td.isRun){
                System.out.println("break!");
                break;
            }
        }
    }
}
 class ThreadDemo1 implements Runnable {

    public  boolean isRun = false;



     @Override
     public void run() {
         try {
             Thread.sleep(2000);
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
         isRun = true;
         System.out.println("isRun is seted true");
     }
 }
